package com.stox.services.charting.repository;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.stox.services.core.repository.AbstractCachingFileRepository;
import com.stox.shared.charting.model.SavedChart;
import com.stox.shared.core.util.PathUtil;
import com.stox.shared.core.util.StringUtil;

public class SavedChartFileRepository extends AbstractCachingFileRepository<SavedChart> {

	public static final SavedChartFileRepository INSTANCE = new SavedChartFileRepository();
	
	private SavedChartFileRepository() {
		super("saved-chart");
	}
	
	public String getPath(SavedChart savedChart){
		return getPath(savedChart.getChartGroupId(), savedChart.getId());
	}
	
	private String getPath(Long chartGroupId, Long savedChartId){
		return PathUtil.getRootPath()+"filebase"+File.separator+"saved-chart"+
				File.separator+chartGroupId+File.separator+
				savedChartId+".xml";
	}

	@Override
	public Long saveOrUpdate(SavedChart savedChart) {
		super.saveOrUpdate(savedChart);
		if(StringUtil.hasText(savedChart.getContent())){
			String path = getPath(savedChart);
			File file = new File(path);
			if(!file.exists()){
				file.getParentFile().mkdirs();
				try {
					file.createNewFile();
				} catch (IOException e) {
					e.printStackTrace();
					throw new RuntimeException(e);
				}
			}
			
			FileWriter fw;
			try {
				fw = new FileWriter(file, false);
				fw.write(savedChart.getContent());
				fw.close();
			} catch (IOException e) {
				e.printStackTrace();
				throw new RuntimeException(e);
			}
		}
		return savedChart.getId();
	}
	
	public String load(Long chartGroupId, Long savedChartId){
		try {
			BufferedReader bufferedReader = new BufferedReader(new FileReader(getPath(chartGroupId, savedChartId)));
			String line = null;
			StringBuffer stringBuffer = new StringBuffer();
			while(null != (line = bufferedReader.readLine())){
				stringBuffer.append(line);
			}
			bufferedReader.close();
			return stringBuffer.toString();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		} catch (IOException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}
	
	public List<SavedChart> findByChartGroupId(Long id) {
		List<SavedChart> savedCharts = new ArrayList<SavedChart>();
		for(SavedChart savedChart : findAll()){
			if(id.equals(savedChart.getChartGroupId())){
				savedCharts.add(savedChart);
			}
		}
		return savedCharts;
	}
	
	@Override
	public SavedChart deserialize(String text) {
		SavedChart savedChart = new SavedChart();
		String[] tokens = text.split(",");
		savedChart.setId(Long.parseLong(tokens[0]));
		savedChart.setChartGroupId(Long.parseLong(tokens[1]));
		savedChart.setPrimaryScripId(Long.parseLong(tokens[2]));
		savedChart.setName(tokens[3]);
		return savedChart;
	}

	@Override
	public String serialize(SavedChart savedChart) {
		return 	savedChart.getId()+","+
				savedChart.getChartGroupId()+","+
				String.valueOf(savedChart.getPrimaryScripId())+","+
				savedChart.getName();
	}

}
